package com.bootdo.system.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.system.domain.Address;
import com.bootdo.system.domain.SearchHistory;
import com.bootdo.system.service.AddressService;
import com.bootdo.system.service.SearchHistoryService;
import com.bootdo.system.util.MapUtil;
import com.bootdo.system.util.QRCodeUtil;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RequestMapping("/sys/address")
@Controller
@Api(value = "/sys/address", description = "前台用户地址地址操作接口")
public class AddressController extends BaseController {
	
	@Autowired
	AddressService service;

	@Autowired
	SearchHistoryService searchHistoryService;

	@ApiOperation(value="前台用户地址查询", notes="根据条件查询前台用户地址", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "前台用户地址id", required = false,paramType="query", dataType = "Long"),
        @ApiImplicitParam(name = "linkerName", value = "联系人姓名", required = false,paramType="query", dataType = "String"),
        @ApiImplicitParam(name = "linkerPhone", value = "联系人电话", required = false,paramType="query", dataType = "String"),
        @ApiImplicitParam(name = "addressType", value = "地址类型", required = false,paramType="query", dataType = "Long"),
        @ApiImplicitParam(name = "ownerId", value = "所属用户id", required = false,paramType="query", dataType = "Long"),
        @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true,paramType="query", dataType = "Integer"),
        @ApiImplicitParam(name = "pageNum", value = "当前页数", required = true,paramType="query", dataType = "Integer")
	})
	@GetMapping("/list")
	@ResponseBody
	R list(@RequestParam Map<String,Object> param) throws Exception{
		Query query = new Query(param);
		List<Address> address = service.list(query);
		PageInfo<Address> pageInfo = new PageInfo<Address>(address);
		int total = (int)pageInfo.getTotal();
		PageUtils pageUtil = new PageUtils(address, total);
		return new R("list",pageUtil);
	}

	@ApiOperation(value="前台用户地址详情", notes="根据条件查询前台用户地址", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "前台用户地址id", required = true,paramType="query", dataType = "Long"),
	})
	@GetMapping("/edit")
	@ResponseBody
	R edit(Long id)  throws Exception{
		return new R("obj",service.get(id));
	}

	@ApiOperation(value="前台用户地址保存", notes="保存前台用户地址", response=String.class)
	@PostMapping("/save")
	@ResponseBody
	R save(@RequestBody Address baby) throws Exception {
		if (service.save(baby) > 0) {
			return R.saveOk(baby.getId().toString());
		} else {
			return R.error(1, "保存失败");
		}
	}

	@ApiOperation(value="前台用户地址更新", notes="更新前台用户地址", response=String.class)
	@PostMapping("/update")
	@ResponseBody
	R update(@RequestBody Address baby)  throws Exception{
		if (service.update(baby) > 0) {
			return R.ok();
		} else {
			return R.error(1, "更新失败");
		}
	}

	@ApiOperation(value="前台用户地址删除", notes="根据前台用户地址id删除", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "前台用户地址id", required = true,paramType="query", dataType = "Long"),
	})
	@GetMapping("/remove")
	@ResponseBody
	R remove(Long id)  throws Exception{
		if (service.remove(id) > 0) {
			return R.ok();
		} else {
			return R.error(1, "删除失败");
		}
	}
	
	
	@ApiOperation(value="根据地名查找经纬度", notes="", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "placeName", value = "地名", required = true,paramType="query", dataType = "String"),
        @ApiImplicitParam(name = "userId", value = "用户id", required = true,paramType="query", dataType = "Long")
	})
	@GetMapping("/getLocationByName")
	@ResponseBody
	R getLocationByName(@RequestParam Map<String,Object> param)  throws Exception{
		SearchHistory sh = new SearchHistory();
		sh.setCreateTime(new Date());
		sh.setUserId(Long.parseLong(param.get("userId")+""));
		sh.setSearchContent(param.get("placeName")+"");
		searchHistoryService.save(sh);
		return R.ok(MapUtil.getLocationByName(param.get("placeName")+""));
	}
	
	@ApiOperation(value="根据经纬度查找地名", notes="", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "lng", value = "经度值", required = true,paramType="query", dataType = "String"),
        @ApiImplicitParam(name = "lat", value = "纬度值", required = true,paramType="query", dataType = "String"),
        @ApiImplicitParam(name = "userId", value = "用户id", required = true,paramType="query", dataType = "Long")
	})
	@GetMapping("/getName")
	@ResponseBody
	R getName(String lng, String lat)  throws Exception{
		return R.ok(MapUtil.getName(lng,lat));
	}
	
}
